package myaction.teacher;

import myutil.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class TeacherChangePassword extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String new_password = request.getParameter("new_password");
        String s_id = String.valueOf(session.getAttribute("teacher_id"));
        String sql = "update t_teachers set t_password=? where t_id=?;";
        try {
            Connection conn = DbUtil.getConn();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, new_password);
            ps.setString(2, s_id);
            ps.executeUpdate();
//            request.setAttribute("mess","<script>alert('密码已修改！');</script>");
            request.getRequestDispatcher("teacher/teachermain.jsp").forward(request, response);
            ps.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

